package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityWechatAppInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.company.GetGzhListOfComRequest;
import com.ocom.common.request.company.GetXcxListOfComRequest;
import com.ocom.company.dao.IDaoWechatAppInfo;
import com.ocom.company.service.WechatAppInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WechatAppInfoServiceImpl extends ServiceImpl<IDaoWechatAppInfo, EntityWechatAppInfo> implements WechatAppInfoService {


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
	public Boolean addInfo(EntityWechatAppInfo entityWechatAppInfo) {
		UpdateWrapper<EntityWechatAppInfo> updateWrapper=new UpdateWrapper();
		updateWrapper.eq("com_id" , entityWechatAppInfo.getComId());
		updateWrapper.eq("merch_id" , entityWechatAppInfo.getMerchId());
		updateWrapper.set("appid",entityWechatAppInfo.getAppid());
		updateWrapper.set("appsecret",entityWechatAppInfo.getAppsecret());
		updateWrapper.set("app_name",entityWechatAppInfo.getAppName());
		return this.saveOrUpdate(entityWechatAppInfo,updateWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
	public Boolean setAppInfo(EntityWechatAppInfo entityWechatAppInfo) {
		QueryWrapper<EntityWechatAppInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id" , entityWechatAppInfo.getComId());
		queryWrapper.eq("appid" , entityWechatAppInfo.getAppid());
		queryWrapper.last("limit 1");
		Integer count = this.count(queryWrapper);
		if(entityWechatAppInfo.getIsAgent() == 1){
			entityWechatAppInfo.setMerchId(-10l);
		}else{
			entityWechatAppInfo.setMerchId(-1l);
		}
		if(count==0){
			entityWechatAppInfo.setAppName("gzh**"+entityWechatAppInfo.getComId()+"**"+System.currentTimeMillis());
			return this.save(entityWechatAppInfo);
		}
		UpdateWrapper<EntityWechatAppInfo> updateWrapper=new UpdateWrapper();
		updateWrapper.eq("com_id" , entityWechatAppInfo.getComId());
		updateWrapper.eq("appid" , entityWechatAppInfo.getAppid());
//		if(entityWechatAppInfo.getIsAgent() == 1){
//			updateWrapper.set("merch_id",-10l);
//		}else{
//			updateWrapper.set("merch_id",-1l);
//		}
		updateWrapper.set("appsecret",entityWechatAppInfo.getAppsecret());
//		updateWrapper.set("app_name","gzh#"+entityWechatAppInfo.getComId()+"#"+System.currentTimeMillis());
		return this.update(updateWrapper);
	}


	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public EntityWechatAppInfo getAppInfo(Long comId, String appId) {
		QueryWrapper<EntityWechatAppInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id" , comId);
		queryWrapper.eq("appid" , appId);
		queryWrapper.apply("app_name like 'gzh%'" );
		queryWrapper.orderByDesc("update_time");
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public EntityWechatAppInfo getAppInfoByName(String appName) {
		QueryWrapper<EntityWechatAppInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("app_name" , appName);
		queryWrapper.orderByDesc("update_time");
		queryWrapper.last("limit 1");
		return this.getOne(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public List<EntityWechatAppInfo> getGzhListOfCom(GetGzhListOfComRequest request) {
		QueryWrapper<EntityWechatAppInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("com_id" , request.getComId());
		queryWrapper.eq("merch_id" , request.getIsAgent());
		queryWrapper.apply("app_name like 'gzh%'" );
		queryWrapper.orderByDesc("update_time");
		return this.list(queryWrapper);
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
	public List<EntityWechatAppInfo> getXcxListOfCom(GetXcxListOfComRequest request) {
		QueryWrapper<EntityWechatAppInfo> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("appid" , request.getAppid());
		queryWrapper.apply("app_name like 'alixcx%'" );
		queryWrapper.orderByDesc("update_time");
		return this.list(queryWrapper);
	}
}
